# Maintainer: Johannes Schindelin <johannes.schindelin@gmx.de>

_realname=jj
_commit=c59dcd76a4e085de85577df7c515bcf7884c8373
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=0.2.0.586.c59dcd76
pkgrel=1
pkgdesc='Jujutsu (an experimental VCS) (mingw-w64)'
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clangarm64' 'clang32')
url="https://github.com/martinvonz/jj"
license=('spdx:Apache-2.0')
makedepends=("${MINGW_PACKAGE_PREFIX}-rust" "git")
source=("git+https://github.com/martinvonz/jj#commit=${_commit}")
sha256sums=('SKIP')

pkgver() {
  cd "${_realname}"
  test ! -f "$(git rev-parse --git-path shallow)" || git -c http.sslbackend fetch --unshallow
  rev_version="$(git blame -lsL '/^version *=/,+1' Cargo.toml)"
  rev=${rev_version%% *}
  version=${rev_version##*=}
  version="${version%\"}"
  version="${version# \"}"
  printf "%s.%s.%s" "${version// /}" "$(git rev-list --count $rev.. -- .)" "$(git rev-parse --short HEAD)"
}

prepare() {
  if test ! -d build-${MSYSTEM}
  then
    git -C ${_realname} worktree add --detach "$PWD/build-${MSYSTEM}"
  else
    git -C build-${MSYSTEM} reset --hard $(git -C ${_realname} rev-parse HEAD)
  fi
  ${MINGW_PREFIX}/bin/cargo fetch \
    --locked \
	--manifest-path build-${MSYSTEM}/Cargo.toml
}

build() {
  ${MINGW_PREFIX}/bin/cargo build \
	--release \
	--frozen \
	--manifest-path build-${MSYSTEM}/Cargo.toml
}

check() {
  ${MINGW_PREFIX}/bin/cargo test \
	--release \
	--frozen \
	--manifest-path build-${MSYSTEM}/Cargo.toml
}

package() {
  ${MINGW_PREFIX}/bin/cargo install \
	--frozen \
	--offline \
	--no-track \
	--path build-${MSYSTEM} \
	--root ${pkgdir}${MINGW_PREFIX}

  install -Dm644 build-${MSYSTEM}/LICENSE "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
